package com.meow.action.admin.visa;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import net.sf.json.JSONObject;

import com.meow.dao.entity.basic.Users;
import com.meow.dao.entity.visa.Notice;
import com.meow.dao.mapper.visa.NoticeDao;
import com.meow.util.AjaxOut;
import com.meow.util.EntityReflect;
import com.meow.util.ExtLimit;

@Controller
@RequestMapping("/admin/visa/NoticeAction/")
public class NoticeAction {

	@RequestMapping("search.action")
	public void search(HttpServletRequest request, HttpServletResponse response) {
		try {
			Notice notice = (Notice) EntityReflect.createObjectFromRequest(request, Notice.class);
			ExtLimit limit = (ExtLimit) EntityReflect.createObjectFromRequest(request, ExtLimit.class);
			notice.setExtLimit(limit);
			int count = noticeDao.selectLimitCount(notice);
			List list = noticeDao.selectByLimit(notice);
			AjaxOut.responseJSonGrid(response, list, count, new SimpleDateFormat("yyyy-MM-dd"));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("save.action")
	public void save(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			Notice notice = (Notice) EntityReflect.createObjectFromRequest(request, Notice.class);
			Users userEty = (Users) request.getSession().getAttribute("UserEty");
			if(notice.getId() == null) {
				notice.setCreateTime(new Date());
				notice.setCreateUser(userEty.getRealName());
				noticeDao.insert(notice);
			} else { 
				notice.setModifyTime(new Date());
				notice.setModifyUser(userEty.getRealName());
				noticeDao.updateById(notice);
			}
			obj.put("result","success");		}
		catch (Exception e) {
			e.printStackTrace();
		}
		AjaxOut.responseText(response, obj.toString());
	}

	@RequestMapping("delete.action")
	public void delete(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String id = request.getParameter("id");
			noticeDao.deleteById(Integer.parseInt(id));
			obj.put("result","success");
		}
		catch (Exception e) {
			e.printStackTrace();
			obj.put("result","error");
			obj.put("info",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}

	@RequestMapping("getDetailInfo.action")
	public void getDetailInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String id = request.getParameter("id");
			Notice notice = (Notice) noticeDao.selectById(Integer.parseInt(id));
			String dataStr = EntityReflect.getObjectJSonString(notice, new SimpleDateFormat("yyyy-MM-dd"));
			obj.put("data", dataStr);
		}
		catch (Exception e) {
			e.printStackTrace();
			obj.put("result","error");
			obj.put("info",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}

	@Autowired
	private NoticeDao noticeDao;
}
